import {defineStore} from 'pinia';

export const useGlobalStore = defineStore('globalStore', {
    state() {
        return {
            $globalState: {
                $session: sessionStorage.getItem('lc.app.globalState.$session')
                    ? JSON.parse(sessionStorage.getItem('lc.app.globalState.$session') as string)
                    : {
                        token: {
                            mode: 'header', // url
                            name: 'Authorization',
                            value: '',
                        },
                        user: {
                            name: '',
                            roles: [],
                            permissions: [],
                        },
                    },
            },
        };
    },
    actions: {
        $setGlobalState(newState: any) {
            if ('$session' in newState) {
                sessionStorage.setItem('lc.app.globalState.$session', JSON.stringify(newState['$session']));
            }
            Object.assign(this.$globalState, newState);
        },
    },
});
